<script type="text/javascript">
	$(function(){
		$('[name=category_cid]').change(function(){
//			抓取选中的类型id
			var tid = $('option:selected').attr('tid');
			$.ajax({
				type:"post",
				url:"<?php echo U('getTypeAttr')?>",
				data:{id:tid},
				dataType:'json',
				success:function(phpData){
					//规格(可选)
					var spec = '';
					//属性（不可选）
					var attr = '';
					$.each(phpData,function(k,v){
						//规格
						if(v.taclass == 1){
								spec += '<tr class="info"><td>'+v.taname+'</td><td><select name="spec['+v.taid+'][value][]" id=""><option value="">请选择</option>';
								$.each(v.tavalue, function(kk,vv) {    
									spec += '<option value="'+vv+'">'+vv+'</option>';                                                      
										});
									spec += '</select></td><td>附加价格：<input type="text" name="spec['+v.taid+'][price][]" id="" /></td><td><span class="btn btn-success add-spec">增加规格</span></td></tr>';
						}else{
						//属性
						attr += '<tr class="info"><td>'+v.taname+'</td><td><select name="attr['+v.taid+']" id=""><option value="">请选择</option>';
						$.each(v.tavalue, function(kk,vv) {    
							attr += '<option value="'+vv+'">'+vv+'</option>';                                                      
												});
								attr += '</select></td></tr>';
							}
					})
					$('#attr').html(attr);
					$('#spec').html(spec);
				}
			});
		})
		
		//点击添加规格
		$('.add-spec').live('click',function(){
			//找到父级的tr
			var parentsTr = $(this).parents('tr');
			//把父级的tr克隆复制
			var cloneTr = parentsTr.clone();
			cloneTr.find('.add-spec').removeClass('add-spec btn-success').addClass('remove-spec btn-danger').html('删除规格');
			//插入下一行
			parentsTr.after(cloneTr);
		})
		//删除规格
		$('.remove-spec').live('click',function(){
			$(this).parents('tr').remove();
		})
	})
</script>
<div class="alert alert-success">商品规格</div>
	<table class="table table-bordered table-hover" id='spec' class="info">
				
				
				
	</table>
<div class="alert alert-success">商品属性</div>
	<table class="table table-bordered table-hover" id='attr' class="info">
				
				
				
	</table>